package com.finance.rule.request;


import com.finance.base.constant.PatternConstants;
import com.finance.framework.lang.valid.EnumValid;
import com.finance.rule.export.NewWeightRuleImportTemplateBean;
import com.finance.system.service.ICarrierService;
import com.finance.system.service.IRegionCodeService;
import com.finance.system.service.ISupplierService;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Pattern;


@Data
public class NewWeightRuleImportReq {

    @NotBlank(message = "供应商名称不可为空或者不能有误")
    private String supplierCode;

    @NotBlank(message = "承运商名称不可为空或者不能有误")
    private String  carrierCode;

    @NotBlank(message = "目的地省份不可为空或者不能有误")
    private String toProvince;

    @NotBlank(message = "目的市不能有误")
    private String toCityError;
    private Integer toCity;

    //发货省份
    private Integer fromProvince;

    //发货城市
    private Integer fromCity;

    /**
     * 区域类型 枚举 0一类区域 1二类区域 2三类区域
     */
    /*@NotBlank(message = "区域类型不能为空或者有误")*/
    private String regionId;

    //"结算类型"
    private Integer settlementType;

    @Length(max = 11,message = "左边界数值不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "左边界数值最多两位小数")
    private String pointLeft;

    @Length(max = 11,message = "右边界数值不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "右边界数值最多两位小数")
    private String pointRight;

    @Length(max = 11,message = "首重单价不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "首重单价最多两位小数")
    private String firstPrice;

    @Length(max = 11,message = "续重单价不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "续重单价最多两位小数")
    private String secondPrice;

    @Length(max = 11,message = "首重不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "首重最多两位小数")
    private  String firstWeight;

    @Length(max = 11,message = "续重不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "续重最多两位小数")
    private String secondWeight;

    @Length(max = 11,message = "代收款不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "代收款最多两位小数")
    private String cod;

    @Length(max = 11,message = "配送类型不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "配送类型最多两位小数")
    private String direction;

    @Length(max = 11,message = "时效要求不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "时效要求最多两位小数")
    private String expressScheduleType;

    @Length(max = 11,message = "温度级别不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_NUMBER_TWO_DECIMAL_POINT,message = "温度级别最多两位小数")
    private String temperature;

    @Length(max = 11,message = "菜鸟驿站不能超过11位")
    @Pattern(regexp = PatternConstants.PATTERN_DECIMAIL,message = "菜鸟驿站金额格式错误,最多两位小数")
    private String cainiao;

    @EnumValid(enums = {"0","1"},message = "组包枚举型")
    private String isCombined;

    public NewWeightRuleImportReq(){}

    public NewWeightRuleImportReq(NewWeightRuleImportTemplateBean bean,
                                  ISupplierService supplierService,
                                  ICarrierService carrierService,
                                  IRegionCodeService regionCodeService){
        if(StringUtils.isNotBlank(bean.getSupplierCodeImport())){
            String supplierCode = supplierService.getCodeByName(bean.getSupplierCodeImport().trim());
            if(StringUtils.isNotBlank(supplierCode)) {
                this.setSupplierCode(supplierCode);
            }
        }
        if(StringUtils.isNotBlank(bean.getCarrierCodeImport())){
            String carrierCode =  carrierService.getCodeByName(bean.getCarrierCodeImport().trim());
            if(null != carrierCode) {
                this.setCarrierCode(carrierCode);
            }
        }
        if(StringUtils.isNotBlank(bean.getToProvinceImport())){
            String toProvince = regionCodeService.getCodeByName(bean.getToProvinceImport().trim());
            if(StringUtils.isNotBlank(toProvince)) {
                this.setToProvince(toProvince);
            }
            if(StringUtils.isNotBlank(bean.getToCityImport())){
                String toCity = regionCodeService.getCodeByNameAndParent(bean.getToCityImport().trim(),toProvince);
                if(StringUtils.isNotBlank(toCity)) {
                    this.setToCity(Integer.valueOf(toCity));
                    this.setToCityError("false");
                }
            }else {
                this.setToCityError("false");
            }
        }
        if(StringUtils.isNotBlank(bean.getFromProvinceImport())){
            String fromProvinceCode = regionCodeService.getCodeByName(bean.getFromProvinceImport().trim());
            if(StringUtils.isNotBlank(fromProvinceCode)){
                this.setFromProvince(Integer.valueOf(fromProvinceCode));
            }
        }

        if(StringUtils.isNotBlank(bean.getFromCityImport())){
            String fromProvinceCode = String.valueOf(this.getFromProvince());
            String fromCityCode = regionCodeService.getCodeByNameAndParent(bean.getFromCityImport().trim(),fromProvinceCode);
            if(StringUtils.isNotBlank(fromCityCode)){
                this.setFromCity(Integer.valueOf(fromCityCode));
            }
        }

        if(StringUtils.isNotBlank(bean.getRegionIdImport())){
            String level = bean.getRegionIdImport().trim();
            if("一类区域".equals(level) || "1".equals(level)){
                this.setRegionId("0");
            }else if("二类区域".equals(level) || "2".equals(level)){
                this.setRegionId("1");
            }else if("三类区域".equals(level) || "3".equals(level)){
                this.setRegionId("2");
            }
        }
        if(StringUtils.isNotBlank(bean.getSettlementTypeImport())){
            String level = bean.getSettlementTypeImport().trim();
            if("投递员价格".equals(level) || "投递员价".equals(level)){
                this.setSettlementType(Integer.valueOf("0"));
            }else if("合作商价格".equals(level)||"合作商价".equals(level)){
                this.setSettlementType(Integer.valueOf("1"));
            }else if("合作站点价格".equals(level)|| "合作站点价".equals(level)){
                this.setSettlementType(Integer.valueOf("2"));
            }
        }
        this.setPointLeft(bean.getPointLeft());
        this.setPointRight(bean.getPointRight());
        this.setFirstPrice(bean.getFirstPrice());
        this.setSecondPrice(bean.getSecondPrice());
        this.setFirstWeight(bean.getFirstWeight());
        this.setSecondWeight(bean.getSecondWeight());


        this.setCod(StringUtils.isNotBlank(bean.getCod()) ? bean.getCod() :"0");
        this.setDirection(StringUtils.isNotBlank(bean.getDirection()) ? bean.getDirection():"0");
        this.setExpressScheduleType(StringUtils.isNotBlank(bean.getExpressScheduleType()) ? bean.getExpressScheduleType():"0");
        this.setTemperature(StringUtils.isNotBlank(bean.getTemperature()) ? bean.getTemperature():"0");
        this.setCainiao(StringUtils.isNotBlank(bean.getCainiao()) ? bean.getCainiao():"0");

        if(StringUtils.isNotBlank(bean.getIsCombinedImport())){
            String isCombined = bean.getIsCombinedImport().trim();
            if("是".equals(isCombined)){
                this.setIsCombined("1");
            }else if("否".equals(isCombined)){
                this.setIsCombined("0");
            }
        }
    }
}